#include <QikListBoxModel.h>
#include <QikListBox.h>
#include <MQikListBoxData.h>
#include <QikListBoxData.h>
#include <QikContent.h>
#include <QikZoomDlg.h>
#include <QikCommand.h>

#include "PinQPacketsView.h"
#include "PinQDocument.h"
#include "PinQTask.h"
#include "PinQExternalInterface.h"
#include "PinQ.hrh"
#include <PinQ.rsg>
#include <PinQ.mbg>

CPinQPacketsView* CPinQPacketsView::NewLC(CQikAppUi& aAppUi,const TVwsViewId aParentViewId)
	{
	CPinQPacketsView* self = new (ELeave) CPinQPacketsView(aAppUi,aParentViewId);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CPinQPacketsView::CPinQPacketsView(CQikAppUi& aAppUi,const TVwsViewId aParentViewId) 
	: CQikViewBase(aAppUi, aParentViewId)
	{
	}

CPinQPacketsView::~CPinQPacketsView()
	{
	}

void CPinQPacketsView::ConstructL()
	{
	BaseConstructL();
	}
	
TVwsViewId CPinQPacketsView::ViewId()const
	{
	return TVwsViewId(KUidPinQApp, KUidPinQPacketsView);
	}

void CPinQPacketsView::HandleCommandL(CQikCommand& aCommand)
	{
	CPinQAppUi* appUi = static_cast<CPinQAppUi*>(iCoeEnv->AppUi());
	switch(aCommand.Id())
		{
		case EPinQBackCmd:
			{
			TVwsViewId listView = TVwsViewId(KUidPinQApp, KUidPinQListView);
			iQikAppUi.ActivateViewL(listView);
			
			RequestFocusL(NULL);
			break;
			}		
		case EPinQZoomCmd:
			{
			CPinQDocument* document = static_cast<CPinQDocument*>(iQikAppUi.Document());
			const TInt zoomFactor = CQikZoomDialog::RunDlgLD(document->Preferences().ListViewZoomState());
			
			if(document->Preferences().SetListViewZoomState(zoomFactor))
				{
				SetZoomFactorL(zoomFactor);	
				}
			break;
			}
		default:
			CQikViewBase::HandleCommandL(aCommand);
			break;
		}
	}

void CPinQPacketsView::ViewConstructL()
	{
	ViewConstructFromResourceL(R_PINQ_PACKETS_CONFIGURATIONS);
		
	CPinQDocument* document = static_cast<CPinQDocument*>(iQikAppUi.Document());
	SetZoomFactorL(document->Preferences().ListViewZoomState());
	}

_LIT(KPacketMsgFmt, "%d %d.%.1d");			
	
void CPinQPacketsView::ViewActivatedL(const TVwsViewId& aPrevViewId, const TUid aCustomMessageId,const TDesC8& aCustomMessage)
	{
  //ensure called in right way
	ASSERT(aPrevViewId.iAppUid == KUidPinQApp);
	ASSERT(aCustomMessageId == KUidPinQDnlItem);
  //unpack input data
	TPinQDnlItemBuf buf;
	buf.Copy(aCustomMessage);
	iCurrentTaskUid = buf().iUid;
	const CPinQTask& task=PinQModel().Task(iCurrentTaskUid);
	//start work with listbox
	CQikListBox* listBox = LocateControlByUniqueHandle<CQikListBox>(EPinQPacketsViewListCtrl);

	MQikListBoxModel& model(listBox->Model());
	//remove data from previous activation
	listBox->RemoveAllItemsL();
	//add packetst from task
	model.ModelBeginUpdateLC();
	for (CPinQTask::TPacket* p=task.FirstPacket();p;p=p->iNext)
		{
		TBuf<256> buf;
		MQikListBoxData* listBoxData = model.NewDataL(MQikListBoxModel::EDataNormal);
		CleanupClosePushL(*listBoxData);
		buf.Format(KPacketMsgFmt,p->iSeq,p->iTime/10,p->iTime % 10);
		listBoxData->AddTextL(buf,EQikListBoxSlotText1);
		CleanupStack::PopAndDestroy(listBoxData);
		}
	if (task.PacketsCount()) listBox->SetCurrentItemIndexL(task.PacketsCount()-1, ETrue, EDrawNow);
	model.ModelEndUpdateL();
	}


void CPinQPacketsView::PacketRecievedL(const CPinQTask::TPacket& aPacket)
	{
	if (iCurrentTaskUid!=aPacket.iTask->Uid())
		return;
	
	CPinQTask* task=aPacket.iTask;
	ASSERT(task);
	
	CQikListBox* listBox = LocateControlByUniqueHandle<CQikListBox>(EPinQPacketsViewListCtrl);

	MQikListBoxModel& model(listBox->Model());
	model.ModelBeginUpdateLC();
				
	MQikListBoxData* listBoxData = model.NewDataL(MQikListBoxModel::EDataNormal);
	CleanupClosePushL(*listBoxData);

	if (!task->Pref().Endless())
		if (task->Pref().Loop())
			if (task->PacketsCount()==model.Count())
				model.RemoveDataL(0);

	TBuf<256> buf;
	buf.Format(KPacketMsgFmt,aPacket.iSeq,aPacket.iTime/10,aPacket.iTime % 10);
	listBoxData->AddTextL(buf,EQikListBoxSlotText1);
	CleanupStack::PopAndDestroy(listBoxData);
	listBox->SetCurrentItemIndexL(aPacket.iTask->PacketsCount()-1, ETrue, EDrawNow);
	model.ModelEndUpdateL();
	}

void CPinQPacketsView::SetZoomFactorL(TInt aZoomLevel)
	{
	const TInt zoomFactor = CQikAppUi::ZoomFactorL(aZoomLevel, *iEikonEnv);
	CQikViewBase::SetZoomFactorL(zoomFactor);	
	}

// vim: ts=2:sw=2
